* Table 6 Panel B

clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

capture log close
log using "`root'\Code\Logs\Final_code_RCFS\Table6_PanelB.log", replace
display "$S_TIME  $S_DATE"

set more off
set varabbrev off, perm

timer on 1

local finalfile_yearly "`root'\Data\Final\Finalfile_yearly.dta" 


use `finalfile_yearly', clear
merge m:1 instkey using "`root'\Data\Distance_to_default_2006.dta"
drop _merge
merge 1:1 cusip6 year using "`root'\Data\Institutional_ownership_types_detailed.dta", update
drop if _merge == 2
drop _merge

generate a = pctown_insidersSNL_Y/100
drop pctown_insidersSNL_Y
rename a pctown_insidersSNL_Y

generate avg_logassets_sqY = avg_logassetsY*avg_logassetsY
regress avg_logassets_sqY avg_logassetsY
predict sizesq_orth, residuals
label variable avg_logassetsY "Size (log assets)"
label variable sizesq_orth "Size (log assets) squared, orthogonalized"

rename avg_divdivbookvalueY divbookY
rename divpayoutratioYALT divpayoutY
rename avg_divpayoutratioY divpayoutY_SNL
rename divpershare_year divpershY

winsor tpopshY1, generate(tpopshY1W) p(0.01)

generate avg_tassetsW_BN = avg_totassetsWY/(10^9)

generate avg_tassetsW_BN_sq = avg_tassetsW_BN * avg_tassetsW_BN

xtset instkey year

generate HMDA_to_total3 = (loanvalue + L.loanvalue + L2.loanvalue)/((gross_lns + L.gross_lns + L2.gross_lns)/3)
generate HMDA_to_total2 = (loanvalue + L.loanvalue)/((gross_lns + L.gross_lns)/2)

generate lenientV3 = (loanvalue + L.loanvalue + L2.loanvalue) / (appvalue + L.appvalue + L2.appvalue)
generate lenientC3 = (loancount + L.loancount + L2.loancount) / (appcount + L.appcount + L2.appcount)

generate lenientV2 = (loanvalue + L.loanvalue) / (appvalue + L.appvalue)
generate lenientC2 = (loancount + L.loancount) / (appcount + L.appcount)

foreach var of varlist HMDA_to_total3 lenientV3 lenientC3 {
	generate chg_`var' = D3.`var'
	}

foreach var of varlist HMDA_to_total2 lenientV2 lenientC2 {
	generate chg_`var' = D2.`var'
}


// Make a new variable that captures analyst coverage, including zeroes

generate numest_zeroes = numest
replace numest_zeroes = 0 if numest == .

// Analyst dummy

generate analyst_dummy = 0
replace analyst_dummy = 1 if numest != .

// Generate lag variables to line these up with the 2004-2006 period (since we sort on year 2008 in the quantiles)

generate L2HMDA_to_total3 = L2.HMDA_to_total3 
generate L2lenientV3 = L2.lenientV3


foreach var in fedfunds_balance {
	winsor `var', generate(`var'W) p(0.01)
	label variable `var'W "`var' winsorized at the 1%"
	}

foreach var in fedfunds_balance fedfunds_balanceW {
	by instkey year: egen avg_`var'Y = mean(`var')
	by instkey year: gen `var'4 = `var'[4]  	// The 4th quarter value of the variable in each year
	}

generate avg_fedfunds_repos_scaledWY = (avg_reposWY + avg_fedfunds_balanceWY)/avg_totassetsWY
winsor avg_fedfunds_repos_scaledWY, generate(helper) p(0.01)
drop avg_fedfunds_repos_scaledWY
rename helper avg_fedfunds_repos_scaledWY
label variable avg_fedfunds_repos_scaledWY "Total Fed funds + repos scaled by total assets"

label variable avg_tassetsW_BN "Total assets (bn $)"
label variable avg_tassetsW_BN_sq "Total assets squared (bn $)"
label variable avg_mtbWY "Market to book ratio"
label variable avg_roaaWY "ROA"
label variable earningsvol "Earnings volatility"
label variable lev_fps "Leverage"
label variable avg_rwcapitalratioWY "Risk-weighted capital ratio"
label variable numest_zeroes "Number of analysts"

generate zero_divgr = (neg_divgr == 0 & pos_divgr == 0)

generate divpolicy = .
replace divpolicy = 1 if neg_divgr == 1
replace divpolicy = 2 if zero_divgr == 1
replace divpolicy = 3 if pos_divgr == 1


foreach var in avg_logassetsY sizesq_orth avg_tassetsW_BN avg_tassetsW_BN_sq avg_mtbWY avg_roaaWY lev_fps avg_tieroneratioWY ///
earningsvol avg_int_to_nonintWY pctown_insidersSNL_Y instown_perc x_npr_countY numest_zeroes ///
			avg_stratioY HMDA_to_total3 lenientV3 avg_fedfunds_repos_scaledWY tarpever ///
			ded_own_perc qix_own_perc tra_own_perc top10inst_own_perc instblk_own_perc ///
			nr_inst_blk_owners {

			generate L`var' = L.`var'

			}

local addrhs Lded_own_perc Lqix_own_perc Ltra_own_perc Lnr_inst_blk_owners Lpctown_insidersSNL_Y Lx_npr_countY

local rhs Lavg_logassetsY Lsizesq_orth  Lavg_mtbWY Lavg_roaaWY Llev_fps Lavg_tieroneratioWY  ///
Lavg_stratioY Learningsvol ///
			LlenientV3 ///
			Ltarpever disttodef2006 Lavg_int_to_nonintWY Linstown_perc ///
			Lnumest_zeroes 

foreach inst in `addrhs' {

	mlogit divpolicy `inst' `rhs' if (year == 2007 | year == 2008), cluster(instkey)  baseoutcome(2)  

	mfx2, multiple replace
	outreg2 using Table6_PanelB, ///
		excel cttop(mfx2) stats(coef tstat) par(tstat) ctitle("Dividend change behavior") dec(2) ///
		sortvar(`addrhs' `rhs') ///
		addtext(Sample period, 2007 - 2008) ///
		addnote("Col 1 is decrease, Col 2 is constant, and Col 3 is increase")

	}


display "$S_TIME  $S_DATE"
capture log close
clear all

